// Copyright (c) 2009 Luca Marrocco.
// Modified or written by Luca Marrocco for inclusion with codej.
// Licensed under the Apache License, Version 2.0 (the "License")
package codej.parser;

import java.io.*;

public class PathStrategies {

	private static class ClassStrategy implements PathStrategy {
		public String pathTo(Object object) {
			Class aClass = (Class) object;
			String nameOfClass = aClass.getName();
			return toFilePath(nameOfClass);
		}
	}

	private static class ObjectStrategy implements PathStrategy {
		public String pathTo(Object object) {
			String className = object.getClass().getName();
			return toFilePath(className);
		}
	}

	private static class StringStrategy implements PathStrategy {
		public String pathTo(Object object) {
			String sourcePath = (String) object;
			return toFilePath(sourcePath);
		}
	}

	public static final PathStrategy pathStrategyFor(Object object) {
		if (object instanceof Class)
			return new ClassStrategy();
		if (object instanceof String)
			return new StringStrategy();
		return new ObjectStrategy();
	}

	private static String toFilePath(String string) {
		if (string.endsWith(".java"))
			string = string.replaceAll(".java$", "");
		return string.replaceAll("\\.", File.separator).concat(".java");
	}

}
